package com.galaxy.product.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.galaxy.common.core.utils.MapstructUtils;
import com.galaxy.common.core.utils.StringUtils;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.product.domain.ProductFavorite;
import com.galaxy.product.domain.bo.ProductFavoriteBo;
import com.galaxy.product.domain.vo.ProductFavoriteVo;
import com.galaxy.product.mapper.ProductFavoriteMapper;
import com.galaxy.product.service.IProductFavoriteService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品收藏Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class ProductFavoriteServiceImpl implements IProductFavoriteService {

    private final ProductFavoriteMapper baseMapper;

    /**
     * 分页查询商品收藏列表
     */
    @DS("slave")
    @Override
    public TableDataInfo<ProductFavoriteVo> queryPageList(ProductFavoriteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductFavorite> lqw = buildQueryWrapper(bo);
        Page<ProductFavoriteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品收藏列表
     */
    @DS("slave")
    @Override
    public List<ProductFavoriteVo> queryList(ProductFavoriteBo bo) {
        LambdaQueryWrapper<ProductFavorite> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询商品收藏详情
     */
    @DS("slave")
    @Override
    public ProductFavoriteVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 新增商品收藏
     */
    @Override
    public Boolean insertProductFavorite(ProductFavoriteBo bo) {
        ProductFavorite favorite = MapstructUtils.convert(bo, ProductFavorite.class);
        return baseMapper.insert(favorite) > 0;
    }

    /**
     * 修改商品收藏
     */
    @Override
    public Boolean updateProductFavorite(ProductFavoriteBo bo) {
        ProductFavorite favorite = MapstructUtils.convert(bo, ProductFavorite.class);
        return baseMapper.updateById(favorite) > 0;
    }
    @Override
    public void delete(Long id) {
        baseMapper.deleteById(id);
    }

    /**
     * 删除商品收藏信息
     */
    @Override
    public Boolean deleteWithValidByIds(Long[] ids, Boolean valid) {
        if (valid) {
            // 验证逻辑（如果需要）
        }
        return baseMapper.deleteById(ids) > 0;
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<ProductFavorite> buildQueryWrapper(ProductFavoriteBo bo) {
        LambdaQueryWrapper<ProductFavorite> lqw = new LambdaQueryWrapper<>();
        lqw.eq(bo.getUserId() != null, ProductFavorite::getUserId, bo.getUserId());
        lqw.eq(bo.getProductId() != null, ProductFavorite::getProductId, bo.getProductId());
        lqw.orderByDesc(ProductFavorite::getCreateTime);
        return lqw;
    }

}
